package public

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

//GenTalkFile 制作演讲视频
//请不要用过超过1M的图片，不然生产非常非常慢
//outPath 合成Mp4视频文件
//mp4Pic  原始视频来源
//mp3Path
func GenTalkFile(mp3Path string, mp4Path string, outPath string) bool {

	var mp3PathTemp string = "F:\\voice\\test.mp3"
	var ffm FfmPegTool

	//声音来源：
	//如果声音已经合成

	//输入：已经是mp3
	if strings.Contains(mp3Path, ".mp3") == true {
		mp3PathTemp = mp3Path
	} else if strings.Contains(mp3Path, ".txt") == true || strings.Contains(mp3Path, ".md") == true {
		//输入的是文件
		fileMsg := ReadFullTxtData(mp3Path)
		TextToSpeech(fileMsg, mp3PathTemp)
	} else {
		//输入的是纯 文本
		TextToSpeech(mp3Path, mp3PathTemp)
	}
	fmt.Println(mp3PathTemp)

	//图像来源：
	//如果输入的是图片
	if strings.Contains(mp4Path, ".jpg") == true || strings.Contains(mp4Path, ".PNG") == true || strings.Contains(mp4Path, ".png") == true || strings.Contains(mp4Path, ".JPG") == true {
		ffm.OnePicWithMP3toMp4(mp4Path, mp3Path, outPath)

		return true
	}

	return false
}

//ReadFullTxtData 读取文本数据
func ReadFullTxtData(path string) string {
	file, err := os.Open(path)
	if err != nil {
		fmt.Println(err)
	}
	defer file.Close()

	var buffer []string
	scanner := bufio.NewScanner(file)
	var lines string

	for scanner.Scan() {
		lines = scanner.Text()
		// if strings.Contains(lines, "---") == true {
		// 	//fmt.Println("-------- ")
		// 	break
		// }
		//fmt.Println(lines)
		buffer = append(buffer, lines)

	}
	if len(buffer) == 0 {
		return ""
	}

	var result string

	for _, data := range buffer {
		result += data
		result += "\r\n"
	}
	fmt.Println(result)
	return result

}
